#pragma once

#include "tree.h"

#define NUMS_PERMUTATION_NUMS (4 * 3 * 2 * 1)

extern const u8_s g_order_aa[NUMS_PERMUTATION_NUMS][NUM_NUMS];

// 对外提供：gen_perm_data结构体
typedef struct {
	u8_s perm_no;
} gen_perm_data_t;

static inline void gen_perm_set_num_by_num_perm(tree34_t* tree_p,
						const i8_s* num_b,
						const u8_s* perm_b,
						u32_s num_no) {
	// 要填入的数字位于num_b的几号下标
	u32_s th = perm_b[num_no];

	u32_s num = num_b[th];
	tree_set_num(tree_p, num_no, num);
}

static inline void gen_perm_set_num(tree34_t* tree_p, num_group_s num_group,
				    u32_s perm_no) {
	const u8_s* perm_b = g_order_aa[perm_no];
	const i8_s* num_b = num_group.num_a;
	gen_perm_set_num_by_num_perm(tree_p, num_b, perm_b, 0);
	gen_perm_set_num_by_num_perm(tree_p, num_b, perm_b, 1);
	gen_perm_set_num_by_num_perm(tree_p, num_b, perm_b, 2);
	gen_perm_set_num_by_num_perm(tree_p, num_b, perm_b, 3);
}

// 对外提供：gen_perm_init()
static inline void gen_perm_init(gen_perm_data_t* data_p, tree34_t* tree_p,
				 num_group_s num_group) {
	data_p->perm_no = 0;
	gen_perm_set_num(tree_p, num_group, 0);
}

// 对外提供：gen_perm_has_next()
static inline b32_s gen_perm_has_next(const gen_perm_data_t* data_p) {
	return data_p->perm_no < NUMS_PERMUTATION_NUMS - 1;
}

// 对外提供：gen_perm_next()
static inline void gen_perm_next(gen_perm_data_t* data_p, tree34_t* tree_p,
				 num_group_s num_group) {
	u32_s prev_perm_no = data_p->perm_no;
	u32_s new_perm_no = prev_perm_no + 1;
	data_p->perm_no = new_perm_no;
	gen_perm_set_num(tree_p, num_group, new_perm_no);
}
